<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<script>
    function round(num1, n) {
        var num1 = prompt('请输入你要转换的数字(四舍六入五留双)'); // 可以函数外获得
        var num = parseFloat(num1);
        var n = prompt('请输入你要保留的小数点后位数,例:2相当于保留到小数点后两位\n注意:输入的数是必须是正整数') - 0; //需要省略到的小数点后n位;及n=2相当于保留到小数点后两位;//可以函数外获得
        while ((num1 + '').length !== (num + '').length || n <= 0 || (n + '').length !== (parseInt(n) + '')
            .length) {
            alert('输入有误,请重新输入');
            num1 = prompt('请输入你要转换的数字(四舍六入五留双)');
            n = prompt('请输入你要保留的小数点后位数,例:2相当于保留到小数点后两位') - 0;
            num = parseFloat(num1);
            // return '输入有误,请重新输入';
            // false;
        }
        var num2 = 1;
        for (let i = 0; i < n; i++) {
            num2 *= 10;
        }
        num *= num2
        switch (num > 0) {
            case true:
                if (num * 100 % 100 > 50) {
                    num = parseInt(num) + 1;
                } else if (num * 100 % 100 < 50) {
                    num = parseInt(num);
                } else {
                    parseInt(num) % 2 === 0 ? num = parseInt(num) : num = parseInt(num) + 1;
                }
                break;
            case false:
                num = -num;
                if (num * 100 % 100 > 50) {
                    num = parseInt(num) + 1;
                } else if (num * 100 % 100 < 50) {
                    num = parseInt(num);
                } else {
                    parseInt(num) % 2 === 0 ? num = parseInt(num) : num = parseInt(num) + 1;
                }
                num = -num;
                break;
        }
        return num /= num2;
    }

    alert(round());
</script>

<body>

</body>

</html>